<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use app\common\model;
use think\Response;
use think\Db;

class SalesOffline extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    protected function filter(&$map)
    {
        if ($this->request->param("sales_id")) {
            $map['sales_id'] = ["like", "%" . $this->request->param("sales_id") . "%"];
        }
        if ($this->request->param("offline_id")) {
            $map['offline_id'] = ["like", "%" . $this->request->param("offline_id") . "%"];
        }
        if ($this->request->param("create_time")) {
            $map['create_time'] = ["like", "%" . $this->request->param("create_time") . "%"];
        }
        if ($this->request->param("update_time")) {
            $map['update_time'] = ["like", "%" . $this->request->param("update_time") . "%"];
        }
        if ($this->request->param("isdelete")) {
            $map['isdelete'] = ["like", "%" . $this->request->param("isdelete") . "%"];
        }
    }


    /**
     * 查询所有下线信息
     */
    public function index() {
        $data = $this->request->except(['id']);
        array_shift($data);
        $where = 'where so.isdelete = '.$this::$isdelete.' ';
        $order = 'order by id';
        $sort = ' asc';
        $limit = '';
        $p = '';
        foreach($data as $k=>$v) {
            if(!empty($v)) {
                if('sales_name' == $k) {
                    $where .= " and s.realname like '%".$v."%'";
                }
                if('sales_tel' == $k) {
                    $where .= " and s.mobile like '".$v."%'";
                }
                if('offline_name' == $k) {
                    $where .= " and o.name like '%".$v."%'";
                }
                if('offline_tel' == $k) {
                    $where .= " and o.tel like '".$v."%'";
                }
                if('_sort' == $k) {
                    $sort = $v;
                }
                if('_order' == $k) {
                    // 排序字段 默认为主键名
                    $order = "order by `".$v."` ";
                }
                if('p' == $k) {
                    $p = $v;
                    $limit = " limit ".($v*20).",20 ";
                }
            }
        }

        $sql = 'select s.realname as sales_name,s.mobile as sales_tel, o.name as offline_name,o.tel as offline_tel, so.*
    	from tp_sales_offline as so left join tp_admin_user as s on s.id = so.sales_id left join
      	tp_offline as o on o.id = so.offline_id ';

        $data = Db::query($sql.$where.$order.$sort.$limit);

        $this->view->assign('list', $data);
        $this->view->assign("count", count($data));
        $this->view->assign("page", $p);
        $this->view->assign('numPerPage', 20);
        return $this->view->fetch();
    }

    /**
     * 根据当前用户获取对应下线信息
     */
    public function index_uid() {
        $data = $this->request->except(['id']);
        array_shift($data);

        $where = 'where s.id = '.UID.' and so.isdelete = '.$this::$isdelete.' ';
        $order = 'order by id';
        $sort = ' asc';
        $limit = '';
        $p = '';
        foreach($data as $k=>$v) {
            if(!empty($v)) {
                if('offline_name' == $k) {
                    $where .= " and o.name like '%".$v."%'";
                }
                if('offline_tel' == $k) {
                    $where .= " and o.tel like '".$v."%'";
                }
                if('_sort' == $k) {
                    $sort = $v;
                }
                if('_order' == $k) {
                    $order = "order by `".$v."` ";
                }
                if('p' == $k) {
                    $p = $v;
                    $limit = " limit ".($v*20).",20 ";
                }
            }
        }

        $sql = 'select  s.realname as sales_name,s.mobile as sales_tel, o.name as offline_name,o.tel as offline_tel, so.*
    	from tp_sales_offline as so left join tp_admin_user as s on s.id = so.sales_id left join
      	tp_offline as o on o.id = so.offline_id ';

        $data = Db::query($sql.$where.$order.$sort.$limit);

        $this->view->assign('list', $data);
        $this->view->assign("count", count($data));
        $this->view->assign("page", $p);
        $this->view->assign('numPerPage', 20);
        return $this->view->fetch();
    }

    /**
     * 获得纯数据
     */
    public function get_list() {
        $data = $this->request->except(['id']);
        array_shift($data);
        $where = 'where so.isdelete = '.$this::$isdelete.' ';
        $order = 'order by id';
        $sort = ' asc';
        $limit = '';
        $p = '';
        foreach($data as $k=>$v) {
            if(!empty($v)) {
                if('sales_realname' == $k) {
                    $where .= " and s.realname like '%".$v."%'";
                }
                if('sales_tel' == $k) {
                    $where .= " and s.mobile like '".$v."%'";
                }
                if('offline_name' == $k) {
                    $where .= " and o.name like '%".$v."%'";
                }
                if('offline_tel' == $k) {
                    $where .= " and o.tel like '".$v."%'";
                }
                if('_sort' == $k) {
                    $sort = $v;
                }
                if('_order' == $k) {
                    // 排序字段 默认为主键名
                    $order = "order by `".$v."` ";
                }
                if('p' == $k) {
                    $p = $v;
                    $limit = " limit ".($v*20).",20 ";
                }
            }
        }

        $sql = 'select s.realname as sales_name,s.mobile as sales_tel, o.name as offline_name,o.tel as offline_tel, so.*
    	from tp_sales_offline as so left join tp_admin_user as s on s.id = so.sales_id left join
      	tp_offline as o on o.id = so.offline_id ';

        $data = Db::query($sql.$where.$order.$sort.$limit);
        return ajax_return($data);
    }

}
